{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "00dc9e3e",
   "metadata": {},
   "source": [
    "### 字典 （{key:value}）\n",
    " 1. 结构：key:value（键值对）\n",
    " 2. 字典是无序的，我们通过键(key)去查找值(value)，就好像list用索引(index)查找值(value)一样，只不过字典的“索引”被定义了名字\n",
    " 3. 字典对key的定义有一定的要求：key是由\"不可变\"的数据结构才可以定义，list是不能作为key来定义的。\n",
    " 4. 字典的key是唯一的，字典的值value也可以不唯一。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "6006ead6",
   "metadata": {},
   "outputs": [],
   "source": [
    "users_dict = {\n",
    "    \"cc\":[20,\"IT\"],\n",
    "    \"vv\":[20,\"IT\"]\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "27c050e8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[20, 'IT']"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "users_dict[\"vv\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1dbc05b0",
   "metadata": {},
   "source": [
    "## 字典的创建\n",
    "1. info = {key1:value1 , key2:value2 , ...}直接按照字典格式创建\n",
    "2. info_02 = dict(key1=value1 , key2=value2 , ... ) 使用dict()进行字典创建\n",
    "3. 运用字典增加的方式新建字典:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "cf3467d3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu10001': 'xiaoming', 'stu10002': 'lihua', 'stu10003': 'xiaohong'}\n",
      "<class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "info = {\n",
    "    \"stu10001\":\"xiaoming\",\n",
    "    \"stu10002\":\"lihua\",\n",
    "    \"stu10003\":\"xiaohong\",\n",
    "#     \"stu10004\":\"xiaohong\",\n",
    "}\n",
    "print(info)\n",
    "print(type(info))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "a5ca87bd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu10001': 'xiaoming', 'stu10002': 'lihua', 'stu10003': 'xiaohong'}\n",
      "<class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "info_02 = dict(\n",
    "    stu10001=\"xiaoming\",\n",
    "    stu10002=\"lihua\",\n",
    "    stu10003=\"xiaohong\",\n",
    ")\n",
    "print(info_02)\n",
    "print(type(info_02))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "acdc5c0f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu10001': 'xiaoming', 'stu10002': 'lihua', 'stu10003': 'xiaohong'}\n",
      "<class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "info_03 = {} # 新建空字典\n",
    "info_03[\"stu10001\"] = \"xiaoming\"\n",
    "info_03[\"stu10002\"] = \"lihua\"\n",
    "info_03[\"stu10003\"] = \"xiaohong\"\n",
    "print(info_03)\n",
    "print(type(info_03))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "afa3f471",
   "metadata": {},
   "source": [
    "## 字典的增、删、改、查\n",
    "1. 字典的增加(和字典的第三种创建方式一致)：\n",
    "* info_03[\"stu10004\"] = \"zhichao\"\n",
    "2. 字典的查询\n",
    "* info_03[key_name]\n",
    "* 注意： 正确的查询方式，应该先查询key是否存在字典当中，如下：\n",
    "1. key_name in info_03  ## 查看是否存在,如果存在，返回结果为 True ，不存在为 False\n",
    "2. info_03[key_name]\n",
    "3. 字典的修改\n",
    "1. 查询key是否存在 \n",
    "2. 修改key的值\n",
    "* 例如： 修改info_03中stu10003的值为Alex, if \"stu10003\" in info_03 : info_03 [\"stu10003\"] = \"Alex\"\n",
    "4. 字典的删除\n",
    "* 1.pop(key)\n",
    "* 2.popitem()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "39b62b26",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "------goods detail-------\n",
      "编号0，商品名称： 全家桶，商品价格： 60\n",
      "编号1，商品名称： 笔记本电脑，商品价格： 8000\n",
      "编号2，商品名称： 全牛宴，商品价格： 199\n",
      "编号3，商品名称： 绿茶，商品价格： 8\n",
      "\n",
      "------goods detail-------\n"
     ]
    }
   ],
   "source": [
    "## 购物车项目\n",
    "\n",
    "goods_list = [\n",
    "    [\"全家桶\",60],\n",
    "    [\"笔记本电脑\",8000],\n",
    "    [\"全牛宴\",199],\n",
    "    [\"绿茶\",8]\n",
    "]\n",
    "\n",
    "info = '''------goods detail-------\n",
    "编号0，商品名称： %s，商品价格： %s\n",
    "编号1，商品名称： %s，商品价格： %s\n",
    "编号2，商品名称： %s，商品价格： %s\n",
    "编号3，商品名称： %s，商品价格： %s\n",
    "'''%(goods_list[0][0],goods_list[0][1],\n",
    "     goods_list[1][0],goods_list[1][1],\n",
    "     goods_list[2][0],goods_list[2][1],\n",
    "     goods_list[3][0],goods_list[3][1],\n",
    "    )\n",
    "print(info)\n",
    "\n",
    "while True:\n",
    "    print(\"------goods detail-------\")\n",
    "    break\n",
    "    for i in range(4):  \n",
    "        print(\"编号%s, 商品名称%s：%s元\" %(i,goods_list[i][0],goods_list[i][1]))\n",
    "\n",
    "user_shopping_list = []\n",
    "\n",
    "count = 3\n",
    "while True:\n",
    "    money_balance = 6800\n",
    "    Alipay = int(input(\"请输入您的余额：\"))\n",
    "    count -=1\n",
    "\n",
    "    if Alipay == money_balance:\n",
    "        print(\"您输入的余额正确！\")\n",
    "        choice = int(input(\"请输入购买商品的编号：\"))\n",
    "        ## 1. 输入编号存在，判断余额是否够\n",
    "        if choice < len(goods_list):\n",
    "            ### 1. 余额够，用户余额 - 商品价格\n",
    "            if goods_list[choice][1] <= Alipay:\n",
    "                Alipay = Alipay - goods_list[choice][1]\n",
    "                print(\"您的商品购买成功，已添加到购物车，您的余额还有%s\" % (Alipay))\n",
    "                print(\"购买清单\\n编号%s，商品名称%s：%s元\"%(choice,goods_list[choice][0],goods_list[choice][1]))\n",
    "                break\n",
    "            ### 2. 余额不够，“您的余额不足”\n",
    "            else:\n",
    "                print(\"不好意思，您的余额不足，无法购买\")\n",
    "        ## 2. 输入的编号不存在，“请输入正确的余额”\n",
    "        # 2. 输入错误，“请输入正确的余额”\n",
    "        else:\n",
    "            print(\"您输入的编号不存在，请重新输入！\")\n",
    "            choice_2 = int(input(\"重新输入购买商品的编号：\"))\n",
    "            if choice_2 < len(goods_list):\n",
    "                ### 1. 余额够，用户余额 - 商品价格\n",
    "                if goods_list[choice_2][1] <= Alipay:\n",
    "                    Alipay = Alipay - goods_list[choice_2][1]\n",
    "                    print(\"您的商品购买成功，已添加到购物车，您的余额还有%s\" % (Alipay))\n",
    "                    print(\"购买清单\\n编号%s，商品名称%s：%s元\" % (choice_2, goods_list[choice_2][0], goods_list[choice_2][1]))\n",
    "                    break\n",
    "\n",
    "                ### 2. 余额不够，“您的余额不足”\n",
    "                else:\n",
    "                    print(\"您的余额不足，无法购买\")\n",
    "                break\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "    else:\n",
    "        if count == 0:\n",
    "            print(\"您的账号无法登录！\")\n",
    "            break\n",
    "        else:\n",
    "            print(\"输入错误，您还有%d次机会！\" % count)\n",
    "print(\"over!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d0f41c19",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
